package com.pctx.bibaregisterlogin.service.impl;


import com.pctx.bibacommon.result.Result;
import com.pctx.bibacommon.util.MD5Util;
import com.pctx.bibacommon.util.UuidUtil;
import com.pctx.bibaregisterlogin.dao.CompanyMapper;
import com.pctx.bibaregisterlogin.dao.UserMapper;
import com.pctx.bibaregisterlogin.pojo.Company;
import com.pctx.bibaregisterlogin.pojo.User;
import com.pctx.bibaregisterlogin.service.RegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.UUID;

@Service("registerService")
public class RegisterServiceImpl implements RegisterService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private CompanyMapper companyMapper;

    @Override
    public Result registerUser(String username, String realpassword, String mobile, int type) {
        UUID uuid = UUID.randomUUID();
        User user  = new User();
        user.setUuid(uuid.toString());
        user.setUsername(username);
        user.setRealpassword(realpassword);
        user.setPassword(MD5Util.getMD5(realpassword));
        user.setMobile(mobile);
        user.setType(0);
        Date date = new Date();
        //Timestamp nowdate = new Timestamp(date.getTime());
        user.setCreateTime(date);
        user.setUpdateTime(date);
        int i = userMapper.insert(user);
        if(i!=0){
            return Result.success(user,"注册成功");
        }else{
            return Result.error("注册失败");
        }
    }

    @Override
    public Result registerCompany(String username, String password, String companyName, String companyIndustry, String companyScale, String location, String contactName, String contactPhone) {
        Company company = new Company();
       String uuid= UuidUtil.randomUUID();
        company.setUuid(uuid);
        Date date = new Date();
        company.setCreateTime(date);
        company.setUpdateTime(date);
        company.setUsername(username);
        company.setPassword(MD5Util.getMD5(password));
        company.setRealpassword(password);
        company.setCompanyName(companyName);
        company.setCompanyIndustry(companyIndustry);
        company.setCompanyScale(companyScale);
        company.setLocation(location);
        company.setContactName(contactName);
        company.setContactPhone(contactPhone);
        Boolean flag=companyMapper.insertSelective(company)>0;
        User user=new User();
        user.setUuid(UuidUtil.randomUUID());
        user.setUsername(username);
        user.setPassword(MD5Util.getMD5(password));
        user.setMobile(contactPhone);
        user.setRealpassword(password);
        user.setType(1);
        user.setCompanyUuid(uuid);
        user.setCreateTime(date);
        user.setUpdateTime(date);
        Boolean i = userMapper.insert(user)>0;
        if(flag&&i){
            return Result.success(company,"注册成功");
        }else{
            return Result.error("注册失败");
        }
    }
}
